import { _decorator, Toggle, Node, EditBox } from 'cc';
import { PanelBase } from '../../Basic/UI/PanelBase';
import { LayerType, SoundCode } from '../../Basic/Config/BaseEnum';
import { DataMgr, GameData } from '../../Basic/Manager/DataManager';
import { GameParams } from '../../Basic/Config/DataConfig';
import { AudioManager } from '../../Basic/Manager/AudioManager';
import { MultiPlatform } from '../../Basic/Model/Platform/MultiPlatform';
import { GameModel } from '../Game/GameModel';
import { LevelModel } from '../Level/LevelModel';
const { ccclass, property } = _decorator;

@ccclass('SettingPanel')
export class SettingPanel extends PanelBase {
  panelLayer: LayerType.DialogLayer = LayerType.DialogLayer;
  @property(Toggle)
  musicToggle: Toggle;
  @property(Toggle)
  soundToggle: Toggle;
  @property(Toggle)
  sharkToggle: Toggle;

  @property(Node)
  btn_back: Node;
  @property(Node)
  cdkNode: Node;
  @property(EditBox)
  editNode: EditBox;

  protected onEnable(): void {
    super.onEnable();
    this.renderUI();
  }

  renderUI() {
    this.musicToggle.isChecked = GameData.get(GameParams.gameMusic);
    this.soundToggle.isChecked = GameData.get(GameParams.gameSound);
    this.sharkToggle.isChecked = GameData.get(GameParams.gameShark);
    // this.musicToggle.node.children[0].active = !GameData.get(GameParams.gameMusic);
    // this.soundToggle.node.children[0].active = !GameData.get(GameParams.gameSound);
    // this.sharkToggle.node.children[0].active = !GameData.get(GameParams.gameShark);
    this.btn_back.active = GameModel.Ins().basePanel != null && GameModel.Ins().basePanel.node.parent != null;
  }

  /** 点击音乐复选框 */
  clickMusic() {
    // this.musicToggle.node.children[0].active = !t.isChecked;
    AudioManager.Singleton.playSound(SoundCode.BTN_CLICK);
    DataMgr.changeGameAttr(GameParams.gameMusic, !this.musicToggle.isChecked, true);
    AudioManager.Singleton.loadAndPlayMusic(GameModel.Ins().basePanel && GameModel.Ins().basePanel.node.parent != null ? SoundCode.BGM_GAME : SoundCode.BGM_MAIN);
  }

  /** 点击音乐效复选框 */
  clickSound() {
    // this.soundToggle.node.children[0].active = !t.isChecked;
    AudioManager.Singleton.playSound(SoundCode.BTN_CLICK);
    DataMgr.changeGameAttr(GameParams.gameSound, !this.soundToggle.isChecked, true);
    AudioManager.Singleton.playSound(SoundCode.BTN_CLICK);
    // AudioManager.I.playSound(AudioManager.I.getClick1Code());
  }

  /** 点击震动效复选框 */
  clickShark() {
    // this.sharkToggle.node.children[0].active = !t.isChecked;
    AudioManager.Singleton.playSound(SoundCode.BTN_CLICK);
    DataMgr.changeGameAttr(GameParams.gameShark, !this.sharkToggle.isChecked, true);
    MultiPlatform.Ins().platformUnit.openVibrateShort();
  }

  clickBtn(t: Touch, data) {
    AudioManager.Singleton.playSound(SoundCode.BTN_CLICK);
    const key = data.toString();
    switch (key) {
      case 'backhome':
        this.clickClose();
        GameModel.Ins().basePanel.clickClose();
        LevelModel.Ins().Show();
        break;
      case 'close':
        this.clickClose();
        break;
      case 'cdk':
        this.cdkNode.active = !this.cdkNode.active;
        break;
      case 'sure':
        if (this.editNode.string === '5643186192') {
          this.editNode.string = '';
          DataMgr.changeGameAttr(GameParams.item_box, 10);
          DataMgr.changeGameAttr(GameParams.item_scoop1, 10);
          DataMgr.changeGameAttr(GameParams.item_scoop2, 10);
          if (GameModel.Ins().basePanel != null && GameModel.Ins().basePanel.node.parent != null) GameModel.Ins().propMac.refreshAllProp();
        }
        break;
    }
  }
}
